package com.taxsearch.controller.checkin;

import java.sql.Connection;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.taxsearch.controller.OcrResoluController;
import com.taxsearch.service.linsa.CheckInService;
import com.taxsearch.service.linsa.UpdateRoomCheckinService;
import com.taxsearch.util.AgeCalculation;
import com.taxsearch.util.DbUtil;
import com.taxsearch.util.Json;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



/**
 * 
 * @Title:UpdateRoomCheckinController
 * @Description: 修改在住旅客信息控制器
 * @author 高敏
 * @version V1.0
 * @date 2018年9月14日
 */
@Controller
@RequestMapping("/HotelAndroid")
public class UpdateRoomCheckinController {
	private static Logger logger = Logger.getLogger(UpdateRoomCheckinController.class);
	@Resource
	private UpdateRoomCheckinService updateRoomCheckinService;
	@Resource
	private CheckInService checkInService;

	/**
	 * 查询境内入住信息
	 * 
	 * @return
	 */
	@RequestMapping("/selectCheckin.do")
	@ResponseBody
	public Json selectCheckin(HttpServletRequest request) {
		Json json = new Json();
		try {
			String RECORDSNO = request.getParameter("id");
			System.out.println("前端来的id"+RECORDSNO);
			//RECORDSNO="510122004720180618002438166";
			DbUtil.setDb("1");
			HashMap<String,Object> map = updateRoomCheckinService.selectCheckin(RECORDSNO);
			
			//JDBC查询二进制照片
			Connection connection=DBUtil_Checkin.getConnection();
			HashMap<String,Object> ZHAOPIAN_MAP= DBUtil_Checkin.getByteBlob_Checkin(connection,RECORDSNO);
			byte[] blob=(byte[])ZHAOPIAN_MAP.get("DJ_ZHAOPIAN");
			if(blob!=null){
				String DJ_ZHAOPIAN = CheckInController.getImageString(blob);
				map.put("DJ_ZHAOPIAN", DJ_ZHAOPIAN);
			}
			byte[] blob_UP=(byte[])ZHAOPIAN_MAP.get("DJ_UPZHAOPIAN");
			if(blob_UP!=null){
				String DJ_UPZHAOPIAN = CheckInController.getImageString(blob_UP);
				map.put("DJ_UPZHAOPIAN", DJ_UPZHAOPIAN);
			}
			
			//根据房号查询房费
			HashMap<String,Object> map_m = new HashMap<String,Object>();
			map_m.put("ROOMNUMBER", map.get("DJ_RUZHUFANGHAO"));
			map_m.put("STATIONID", map.get("STATIONID"));
			String MONEY = updateRoomCheckinService.selectRoomMoney(map_m);
			map.put("MONEY", MONEY);
			
			//切换数据源,切换到无证审批数据库
			DbUtil.setDb("3");
			List<HashMap<String,String>> list = checkInService.selectPro_City_Area((String)map.get("DJ_SHENGSHIXIAN"));
			if(!list.isEmpty()){
				map.put("DJ_SHENGSHIXIAN_VALUE", list.get(0).get("DI_CVALUE"));
			}
			map.put("DJ_MINZU_VALUE", updateRoomCheckinService.selectMinZu((String)map.get("DJ_MINZU")));
			
			//切换回默认数据库
			DbUtil.setDb("1");
			
			logger.info("成功查询境内入住信息！");
			json.setObj(map);
			json.setMsg("成功查询境内入住信息！");
			json.setSuccess(true);
		} catch (Exception e) {
			json.setMsg("服务器繁忙,请稍后再试！");
			json.setSuccess(false);
			logger.error("selectCheckin.do-发生错误!!!!!!!!!!!!!!!!!!!!!!!!", e);
			e.printStackTrace();
		}
		return json;
	}
	
	/**
	 * 查询境外入住信息
	 * 
	 * @return
	 */
	@RequestMapping("/selectCheckinF.do")
	@ResponseBody
	public Json selectCheckinF(HttpServletRequest request) {
		Json json = new Json();
		try {
			String RECORDSNO = request.getParameter("id");
			//RECORDSNO="510122006020180912123456309";
			DbUtil.setDb("1");
			HashMap<String,Object> map = updateRoomCheckinService.selectCheckinF(RECORDSNO);
			
			//JDBC查询二进制照片
			Connection connection=DBUtil_Checkin.getConnection();
			HashMap<String,Object> ZHAOPIAN_MAP= DBUtil_Checkin.getByteBlob_CheckinF(connection,RECORDSNO);
			byte[] blob=(byte[])ZHAOPIAN_MAP.get("F_ZHAOPIAN");
			if(blob!=null){
				String F_ZHAOPIAN = CheckInController.getImageString(blob);
				map.put("F_ZHAOPIAN", F_ZHAOPIAN);
			}
			byte[] blob_UP=(byte[])ZHAOPIAN_MAP.get("F_UPZHAOPIAN");
			if(blob_UP!=null){
				String F_UPZHAOPIAN = CheckInController.getImageString(blob_UP);
				map.put("F_UPZHAOPIAN", F_UPZHAOPIAN);
			}

			//根据房号查询房费
			HashMap<String,Object> map_m = new HashMap<String,Object>();
			map_m.put("ROOMNUMBER", map.get("F_ROOM"));
			map_m.put("STATIONID", map.get("STATIONID"));
			String MONEY = updateRoomCheckinService.selectRoomMoney(map_m);
			map.put("MONEY", MONEY);
			
			logger.info("成功查询境外入住信息！");
			json.setObj(map);
			json.setMsg("成功查询境外入住信息！");
			json.setSuccess(true);
		} catch (Exception e) {
			json.setMsg("服务器繁忙,请稍后再试！");
			json.setSuccess(false);
			logger.error("selectCheckinF.do-发生错误!!!!!!!!!!!!!!!!!!!!!!!!", e);
			e.printStackTrace();
		}
		
		return json;
	}
	
	/**
	 * 更新境内入住信息
	 * 
	 * @return
	 */
	@RequestMapping("/updateCheckIn.do")
	@ResponseBody
	public Json updateCheckIn(HttpServletRequest request) {
		Json json = new Json();
		try {
			// 获取前端的值
			String name = request.getParameter("name");
			String sex = request.getParameter("sex");
			String nation = request.getParameter("nation");
			String birth = request.getParameter("birth");
			String IDType = request.getParameter("IDType");
			String IDNumber = request.getParameter("IDNumber");
			String address = request.getParameter("address");
			String time = request.getParameter("time");
			String roomNumber = request.getParameter("roomNumber");
			String photo = request.getParameter("photo");
			String IDPhoto = request.getParameter("IDPhoto");
			String hotelCode = request.getParameter("hotelCode");
			String tel = request.getParameter("tel");
			String xzqh_id = request.getParameter("xzqh_id");
			String is_rzsb = request.getParameter("is_rzsb");
			String RECORDSNO = request.getParameter("id");

			//20211029 jiangf 新增未成年入住功能
			/*--------------------------------------*/
			String guardianName = request.getParameter("jhrxm");
			String guardianIDNumber = request.getParameter("jhrzjh");
			String guardianTel = request.getParameter("jhrdh");
			String residentsRelationship = request.getParameter("tzrgx");
			String is_unreasonable = request.getParameter("gxsfbhl");
			String is_danger = request.getParameter("sfwx");
			String unreasonable_check_in = request.getParameter("bhlrz");
			String other_suspicious_situations = request.getParameter("qtkyqk");
			/*--------------------------------------*/
			
			/*name = "高敏_update";
			sex = "1";
			nation = "01";
			birth = "1994-05-01";
			IDType = "11";
			IDNumber = "421182199405013329";
			address = "111";
			time = "2018-09-12 12:34:56";
			roomNumber = "101";
			hotelCode = "5101220060";
			photo = "111";
			tel = "17345764025";
			xzqh_id = "421182";
			is_rzsb = "1";
			RECORDSNO = "510122004720180618002438166";*/
			
			// base64格式转成Blob二进制
			byte[] photoBytes = null;
			if(photo!=null&&photo!=""){
				photoBytes = CheckInController.getStringImage(photo);
			}
			
			// base64格式转成Blob二进制
			byte[] IDPhotoBytes = null;
			if(IDPhoto!=null&&IDPhoto!=""){
				IDPhotoBytes = CheckInController.getStringImage(IDPhoto);
			}

			String num = (int) (Math.random() * 10) + "" + (int) (Math.random() * 10) + "" + (int) (Math.random() * 10);
			logger.info("RECORDSNO后三位：" + num);
			
			if("0".equals(is_rzsb)) {
				//人证认证
				OcrResoluController ocrCheck= new OcrResoluController();
				if(!ocrCheck.OcrResolutionByPhoto(IDNumber, name, photo, tel)) {//认证验证
					json.setMsg("人证比对服务繁忙！");
					System.out.println("===========================入住认证未通过----");
					json.setSuccess(false);
					return json;
				}
			}
			
			DbUtil.setDb("1");
			// 插入有证入住信息
			HashMap<String, Object> sub_map = new HashMap<String, Object>();
			sub_map.put("DJ_LVKEDAIMA", hotelCode + String.valueOf(CheckInController.generate(12)));
			sub_map.put("DJ_XINGMING", name);
			sub_map.put("DJ_XINGBIE", sex);
			sub_map.put("DJ_MINZU", nation);
			sub_map.put("DJ_CHUSHENGRIQI", birth);
			sub_map.put("DJ_ZHENGJIANLEIXING", IDType);
			sub_map.put("DJ_ZHENGJIANHAOMA", IDNumber);
			sub_map.put("DJ_SHENGSHIXIAN", xzqh_id);
			sub_map.put("DJ_XIANGZHI", address);
			sub_map.put("DJ_RUZHUSHIJIAN", time);
			sub_map.put("DJ_RUZHUFANGHAO", roomNumber);
			sub_map.put("DJ_ZHAOPIAN", IDPhotoBytes);
			sub_map.put("DJ_UPZHAOPIAN", photoBytes);
			sub_map.put("DJ_LVGUANDAIMA", hotelCode);
			sub_map.put("BUPLOAD", "0");
			sub_map.put("RECORDSNO", RECORDSNO);
			sub_map.put("STATIONID", hotelCode);
			sub_map.put("PACKNAME", hotelCode + time.replace("-", "").replace(":", "").replace(" ", "") + num);
			sub_map.put("TEL", tel);
			sub_map.put("ENTERPRISE_ID", "1");
			
			/* 2019/1/21 修改,使用一个字段区分是否使用省公司结构上传同步数据*/
			sub_map.put("DJ_TIMAO", "0");//用于同步数据,0:数据有变化,未同步;1:已同步
			
			logger.info("更新境内入住信息，ID为：" + RECORDSNO);

			//20211029 jiangf 新增未成年入住功能
			/*--------------------------------------*/
			if (!AgeCalculation.getAge(birth)) {//如果是未成年人，则额外记录以下信息
				sub_map.put("DJ_JIANHURENXINGMINGM", guardianName);
				sub_map.put("DJ_JIANHURENZHENGJIAN", guardianIDNumber);
				sub_map.put("DJ_JIANHURENDIANHUA", guardianTel);
				sub_map.put("DJ_TONGZHURENGUANXI", residentsRelationship);
				sub_map.put("DJ_GUANXISHIFOUBUHELI", is_unreasonable);
				sub_map.put("DJ_SHIFOUCHUYUWEIXIAN", is_danger);
				sub_map.put("DJ_BUHELIRUZHU", unreasonable_check_in);
				sub_map.put("DJ_QITAKEYIQINGKUANG", other_suspicious_situations);
			}
			/*--------------------------------------*/
			int sub_res = updateRoomCheckinService.updateCheckIn(sub_map);
			logger.info("成功更新境内入住信息" + sub_res);
			
			if(sub_res==1){
				json.setObj(sub_res);
				json.setMsg("更新成功！");
				logger.info("更新成功！");
				json.setSuccess(true);
			}else{
				json.setObj(sub_res);
				json.setMsg("更新失败！");
				logger.info("更新失败！");
				json.setSuccess(false);
			}
			
		} catch (Exception e) {
			json.setMsg("服务器繁忙,请稍后再试！");
			json.setSuccess(false);
			logger.error("updateCheckIn.do-发生错误!!!!!!!!!!!!!!!!!!!!!!!!", e);
			e.printStackTrace();
		}
		return json;
	}
	
	/**
	 * 更新境外入住信息
	 * @return
	 */
	@RequestMapping("/updateCheckInF.do")
	@ResponseBody
	public Json updateCheckInF(HttpServletRequest request) {
		Json json = new Json();
		try {
			// 获取前端的值
			String hotelCode = request.getParameter("hotelCode");
			String lastname = request.getParameter("lastname");
			String firstname = request.getParameter("firstname");
			String name = request.getParameter("name");
			String sex = request.getParameter("sex");
			String birth = request.getParameter("birth");
			String national = request.getParameter("national");
			String IDType = request.getParameter("IDType");
			String IDNumber = request.getParameter("IDNumber");
			String VisaType = request.getParameter("VisaType");
			String VisaNumber = request.getParameter("VisaNumber");
			String TLQZ_Time = request.getParameter("TLQZ_Time");
			String VisaOffice = request.getParameter("VisaOffice");
			String RJ_Time = request.getParameter("RJ_Time");
			String RJ_Port = request.getParameter("RJ_Port");
			String JD_Company = request.getParameter("JD_Company");
			String RZ_Time = request.getParameter("RZ_Time");
			String RZ_RoomNumber = request.getParameter("RZ_RoomNumber");
			String From_Where = request.getParameter("From_Where");
			String To_Where = request.getParameter("To_Where");
			String IDPhoto = request.getParameter("IDPhoto");
			String photo = request.getParameter("photo");
			String JD_Person = request.getParameter("JD_Person");
			String remark = request.getParameter("remark");
			String TLSY = request.getParameter("TLSY");
			String ID_Time = request.getParameter("ID_Time");
			String birth_Area = request.getParameter("birth_Area");
			String RECORDSNO = request.getParameter("id");
			
			/*hotelCode = "5101220060";
			lastname = "gao_update";
			firstname = "min_update";
			name = "高敏_update";
			sex = "1";
			birth = "1994-05-01";
			national = "USA";
			IDType = "11";
			IDNumber = "421182199405013329";
			VisaType = "v";
			VisaNumber = "3329";
			TLQZ_Time = "2019-09-12";
			VisaOffice = "成都总局";
			RJ_Time = "2018-08-12";
			RJ_Port = "宁波";
			JD_Company = "成都宾馆";
			RZ_Time = "2018-09-12 12:34:56";
			RZ_RoomNumber = "101";
			From_Where = "USA";
			To_Where = "China";
			IDPhoto = "123";
			photo = "123";
			JD_Person = "linsa_update";
			remark = "lala";
			TLSY = "#15";
			ID_Time = "2028-09-12";
			birth_Area = "USA";
			deposit = "30";
			RECORDSNO = "510122006020180912123456309";*/
			
			// base64格式转成Blob二进制
			byte[] photoBytes = null;
			photoBytes = CheckInController.getStringImage(photo);
			// base64格式转成Blob二进制
			byte[] IDPhotoBytes = null;
			IDPhotoBytes = CheckInController.getStringImage(IDPhoto);

			String num = (int) (Math.random() * 10) + "" + (int) (Math.random() * 10) + "" + (int) (Math.random() * 10);
			logger.info("RECORDSNO后三位：" + num);
			
			DbUtil.setDb("1");
			// 插入有证入住信息
			HashMap<String, Object> sub_map = new HashMap<String, Object>();
			sub_map.put("F_LVKEDAIMA", hotelCode + String.valueOf(CheckInController.generate(12)));
			sub_map.put("F_LASTNAME", lastname);
			sub_map.put("F_FIRSTNAME", firstname);
			sub_map.put("F_NAME", name);
			sub_map.put("F_XINGBIE", sex);
			sub_map.put("F_BIRTH", birth);
			sub_map.put("F_GUOJI", national);
			sub_map.put("F_IDTYPE", IDType);
			sub_map.put("F_IDNUMBER", IDNumber);
			sub_map.put("F_QIANZHENGZHONGLEI", VisaType);
			sub_map.put("F_QIANZHENGHAOMA", VisaNumber);
			sub_map.put("F_TINGLIUQIZHI", TLQZ_Time);
			sub_map.put("F_QIANFAJIGUAN", VisaOffice);
			sub_map.put("F_RUJINGRIQI", RJ_Time);
			sub_map.put("F_RUJINGKOUAN", RJ_Port);
			sub_map.put("F_BINGUANID", hotelCode);
			sub_map.put("F_JIEDAIDANWEI", JD_Company);
			sub_map.put("F_RUZHUSHIJIAN", RZ_Time);
			sub_map.put("F_ROOM", RZ_RoomNumber);
			sub_map.put("F_HECHULAI", From_Where);
			sub_map.put("F_QUHECHU", To_Where);
			sub_map.put("F_ZHAOPIAN", IDPhotoBytes);
			sub_map.put("F_UPZHAOPIAN", photoBytes);
			sub_map.put("F_JIEDAIREN", JD_Person);
			sub_map.put("F_BEIZHU", remark);
			sub_map.put("F_CSD", birth_Area);
			sub_map.put("RECORDSNO", RECORDSNO);
			sub_map.put("STATIONID", hotelCode);
			sub_map.put("BUPLOAD", "0");
			sub_map.put("F_TINGLIUSHIYOU", TLSY);
			sub_map.put("F_ZHENGJIANYOUXIAOQI", ID_Time);
			sub_map.put("PACKNAME", hotelCode + RZ_Time.replace("-", "").replace(":", "").replace(" ", "") + num);
			
			/* 2019/1/21 修改,使用一个字段区分是否使用省公司结构上传同步数据*/
			sub_map.put("F_ZHIYE", "0");//用于同步数据,0:数据有变化,未同步;1:已同步
			
			logger.info("更新境外入住信息，ID为：" + RECORDSNO);
			int sub_res = updateRoomCheckinService.updateCheckInF(sub_map);
			logger.info("成功更新境外入住信息" + sub_res);
			
			if(sub_res==1){
				json.setObj(sub_res);
				json.setMsg("更新成功！");
				logger.info("更新成功！");
				json.setSuccess(true);
			}else{
				json.setObj(sub_res);
				json.setMsg("更新失败！");
				logger.info("更新失败！");
				json.setSuccess(false);
			}
			
		} catch (Exception e) {
			json.setMsg("服务器繁忙,请稍后再试！");
			json.setSuccess(false);
			logger.error("updateCheckInF.do-发生错误!!!!!!!!!!!!!!!!!!!!!!!!", e);
			e.printStackTrace();
		}
		return json;
	}
	
}
